package com.neusoft.adminweb.interceptor;

import com.neusoft.adminweb.constant.WebConstant;
import com.sun.javafx.webkit.CursorManagerImpl;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;

/**
 * @Author: duan
 * @Date: Create in 15:38 2020/11/3
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    /**
     *  preHandle
     *      拦截一触发，就会进到该方法，由方法的返回值确定是否放行
     *          false：不通过
     *          true：通过
     *      HttpServletRequest request：请求
     *      HttpServletResponse response：响应
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*
            uri：资源路径包含参数信息
            url：请求路径：包含服务信息及uri所有内容
         */
        String[] whiles = {WebConstant.Whites.LOGIN, WebConstant.Whites.LOGOUT, WebConstant.Whites.TO_LOGIN};
        String requestURI = request.getRequestURI();
        if (Arrays.toString(whiles).contains(requestURI)) {
            return true;
        }
        HttpSession session = request.getSession();
        Object loginAdmin = session.getAttribute(WebConstant.LOGIN_ADMIN);
        if (loginAdmin != null) {
            return true;
        }

        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//        modelAndView.addObject("msg", "postHandle这是拦截器添加的内容");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }
}
